package org.example.service.dataobject;

import lombok.Builder;
import lombok.Data;
import org.example.model.entity.ColumnRelation;
import org.example.model.entity.TableInfo;

import java.util.List;

/**
 * @author wuyong
 * @date 2025/6/13
 */
@Data
@Builder
public class ProjectDataObject {
    private List<TableInfo> tableInfos;
    private List<ColumnRelation> columnRelations;
    public boolean containRelation(ColumnRelation columnRelation){
        if (columnRelation == null) {
            return false;
        }
        // 检查外键表和列
        boolean fkExists = false;
        boolean refExists = false;
        if (tableInfos != null) {
            for (TableInfo table : tableInfos) {
                if (table.getTableName() != null && table.getTableName().equals(columnRelation.getFkTableName())) {
                    if (table.getColumnInfos() != null) {
                        for (org.example.model.entity.ColumnInfo col : table.getColumnInfos()) {
                            if (col.getColumnName() != null && col.getColumnName().equals(columnRelation.getFkColumnName())) {
                                fkExists = true;
                                break;
                            }
                        }
                    }
                }
                if (table.getTableName() != null && table.getTableName().equals(columnRelation.getRefTableName())) {
                    if (table.getColumnInfos() != null) {
                        for (org.example.model.entity.ColumnInfo col : table.getColumnInfos()) {
                            if (col.getColumnName() != null && col.getColumnName().equals(columnRelation.getRefColumnName())) {
                                refExists = true;
                                break;
                            }
                        }
                    }
                }
                if (fkExists && refExists) {
                    break;
                }
            }
        }
        return fkExists && refExists;
    }
}
